const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

var formatDateMonth = function (date) {
  var date = new Date(date)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  console.log(year+"-"+month,date,12321)

  return [year, month].map(formatNumber).join('-')
}
function fatmatTime(dateTime, fmt){
  let date = new Date(dateTime);
  fmt = fmt || 'yyyy-MM-dd';
  let o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;

}
function getFirstDayOfMonth(year,month){
  return new Date(year, month, 1).getTime()
}
function getLastDayOfMonth(year,month){
  return new Date(year, month + 1, 0).getTime()
}
function uuid() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	// s[8] = s[13] = s[18] = s[23] = "-";

	var uuid = s.join("");
	return uuid;
}
 /**
   * 生成指定长度的随机数
   * @param {*} len
   * @returns
   */
function randomString(len) {
  len = len || 5
  var t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  var a = t.length
  var n = ''
  for (var i = 0; i < len; i++) n += t.charAt(Math.floor(Math.random() * a))
  return n
}
function formateIphoneTime(date){
  if(typeof date === 'string'){
    return  date.replace(/-/g,'/');
  }else{
    return date
  }
}
function getFileView(uuid) {
  if (uuid) {
    if (uuid && (uuid.indexOf('http://') === 0 || uuid.indexOf('https://') === 0)) {
      return uuid
    } else {
      return `http://bbs.chiship.cn/bbs/fileView/${uuid}?AppId=638638776006557696&AppKey=61cada1e12324f79bf404f4b3cc6d9d7&ProjectsId=1883442361`
    }
  } else {
    return '-1'
  }
}
 /**
   * 填空题题干处理
   */
function formatQuestionTitle(title){
    return title.replace(/\[BLANK\d\]/ig,'____')
  }
/**
 * 填空题填空
 */
function getAnswerList(answerList){
  if(answerList){
    return JSON.parse(answerList)
  }
}
  /**
 * 倒计时格式转换
 */
function formateCountDown(number){
  var hour = parseInt(number/60)
  var min = number%60
  if(hour<10){
    hour = '0'+hour
  }
  if(min<10){
    min = '0'+min
  }
  return hour+':'+min
}

  /**
   * 生成指定长度的数字
   * @param {*} len   长度
   * @param {*} radix 基数
   * @returns
   */
function randomNumber(len, radix = 10) {
  var chars = '0123456789'.split('')
  var uuid = []
  radix = radix | chars.length
  if (len) {
    for (var i = 0; i < len; i++) {
      uuid[i] = chars[0 | Math.random() * radix]
    }
  }
  return uuid.join('')
}
module.exports = {
  fatmatTime: fatmatTime,
  formatTime: formatTime,
  formatDateMonth:formatDateMonth,
  getFirstDayOfMonth:getFirstDayOfMonth,
  getLastDayOfMonth:getLastDayOfMonth,
  uuid:uuid,
  getFileView:getFileView,
  formateIphoneTime:formateIphoneTime,
  formatQuestionTitle:formatQuestionTitle,
  getAnswerList:getAnswerList,
  formateCountDown:formateCountDown,
  randomNumber:randomNumber,
  randomString:randomString
}
